#include "tmglobaldef.h"

QString g_strRootDir = "";
QString g_strRobotType = "";
QString g_strCoreVersion = "";
QString g_strRobotMAC = "";
QSettings* g_pSettingCore = NULL;
QTextCodec* g_pCodecASCII = NULL;
TmLog* g_pLog = NULL;
TmIceLoader* g_pIceLoader;
TmRadar* g_pRadar = NULL;
bool g_bEnableScreenEx = false;


void g_clearDir(QString strDir)
{
    QDir dirRoot(strDir);
    if( dirRoot.exists() ){
        dirRoot.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
        QFileInfoList fileList = dirRoot.entryInfoList();
        foreach (QFileInfo fi, fileList)
        {
            dirRoot.remove(fi.fileName());
        }
    }
}

void g_clearDirFile(QString strDir, QString ext)
{
    QDir dirRoot(strDir);
    if( dirRoot.exists() ){
        dirRoot.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
        QFileInfoList fileList = dirRoot.entryInfoList();
        foreach (QFileInfo fi, fileList)
        {
            if( fi.suffix() == ext ){
                dirRoot.remove(fi.fileName());
            }
        }
    }
}

QString g_strGetMD5(QByteArray dataArray)
{
    QString strMD5;
    QCryptographicHash md5(QCryptographicHash::Md5);
    md5.addData(dataArray);
    QByteArray bArry = md5.result();
    QString strChar;
    for( int i=0;i<bArry.size();i++ ){
        char cNum = bArry.at(i);
        strChar.sprintf("%02X", (uchar)cNum);
        strMD5 += strChar;
    }
    return strMD5;
}

int g_nGetRandom(int nMin, int nMax)
{
    if(nMin > nMax){
        return -1;
    }
    qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()));
    //int n = (qrand()%((nMax-nMin+1)*100)) / 100 + nMin;
    int n = qrand()%(nMax-nMin+1) + nMin;
    return n;
}

